package edu.gatech.startrader.gui;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import edu.gatech.startrader.universe.Planet;
import edu.gatech.startrader.universe.Universe;

public class UniversePanel extends JPanel 
{
	private Universe universe;
	private double xScale;
	private double yScale;
	private int selectedIndex = -1;
	
	
	/**
	 * Create the panel.
	 */
	public UniversePanel(Universe world) 
	{
		super();
		setBackground(Color.black);
		setEnabled(true);
		setVisible(true);
		universe = world;
		xScale = (double)world.getWidth() / (double)getWidth();
		yScale = (double)world.getHeight() / (double)getHeight();
	}
	
	public void setBounds(int x, int y, int width, int height)
	{
		super.setBounds(x, y, width, height);
		xScale = (double)getWidth() / (double)universe.getWidth();
		yScale = (double)getHeight() / (double)universe.getHeight();
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		double relativeX, relativeY;
		int x, y;
		for(int i = 0; i < universe.length(); i++)
		{
			Planet currentPlanet = universe.getPlanet(i);
			relativeX = currentPlanet.getX() / universe.getWidth();
			relativeY = currentPlanet.getY() / universe.getHeight();
			x = (int)(relativeX * getBounds().width);
			y = (int)(relativeY * getBounds().height);
			if(currentPlanet == universe.getCurrentPlanet())
			{
				g.setColor(Color.yellow);				
				int xOffset = (int)(universe.getTravelDistance() * xScale);
				int yOffset = (int)(universe.getTravelDistance() * yScale);
				g.drawOval(
						x - xOffset, 
						y - yOffset, 
						2 * xOffset,
						2 * yOffset);
				g.setColor(Color.red);
			}
			else if(universe.isTravelablePlanet(universe.getPlanet(i)))
			{
				if(selectedIndex >= 0 && universe.getPlanetInRange(selectedIndex) == universe.getPlanet(i))
					g.setColor(Color.cyan);
				else
					g.setColor(Color.yellow);
			}
			else
				g.setColor(Color.green);
			
			g.fillOval(
					(int)(x - 2.5), 
					(int)(y - 2.5),
					5,
					5);
		}
	}
	
	public void setSelectedIndex(int index)
	{
		selectedIndex = index;
	}
}
